<?php
class SlidesController extends AppController {

	var $name = 'Slides';
	var $components = array('Uploader.Uploader');
	function beforeFilter() {
        parent::beforeFilter();
		$this->Uploader->uploadDir = 'files/uploads/slides/'; //thu muc chua file upload
        $this->Uploader->enableUpload = true;
        $this->Uploader->maxFileSize = '5M'; // quy dinh dung luong duoc upload len toi da la 2 Megabytes
        $this->Uploader->maxNameLength = 25;//do dai cua ten file
        $this->Uploader->tempDir = TMP;
        
    }
	function admin_index() {
		$this->Slide->recursive = 0;  		
 		$this->paginate = array(
			'order'=> 'Slide.id ASC'
		);
 		$this->set('slides', $this->paginate());	
	}	
 	 function admin_add() {		
			//debug($this->data);
			$data = array();			
			if(!empty($this->data)){				
				$upload = $this->Uploader->uploadAll(array('url1','url2','url3','url4','url5'));					
				for($i=1;$i<6;$i++){					
					if(isset($upload['url'.$i])){
						$data['Slide']['url'] =  $upload['url'.$i]['name'];								
						$data['Slide']['note'] = $this->data['Slide']['note'.$i];
						//debug($data);
						$this->Slide->create();
						if ($this->Slide->save($data)){								
							$this->Session->setFlash(__('Đã lưu.', true));					
							//debug($data);
						} else {
							$this->Session->setFlash(__('Có lỗi xảy ra. Hãy thử lại!', true));
						}						
					}					
				}			
				$this->redirect(array('controller' =>'slides','action' => 'index'));
				
			}
			//debug($data['Img']['galleries_id']);
		}
	function _delimg($id = null){
			$files = $this->Slide->find('list',array(
				'conditions'=>array('Slide.id'=>$id),
				'fields'=>'url')
			);
			$file ='';
			$file = "files" . DS . "uploads" . DS. "slides" . DS .$files[$id];
			if(is_file($file)){
				unlink($file);
			}
	}
	function admin_delete($id = NULL) {
		if (!$id) {
			$this->Session->setFlash(__('Không tồn tại ảnh này !', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->_delimg($id);
		if ($this->Slide->delete($id)) {
			$this->Session->setFlash(__('Đã xóa !', true));
			$this->redirect(array('action'=>'index'));
		}
		else{
		$this->Session->setFlash(__('Có lỗi sảy ra. Xóa không thành công !', true));
		$this->redirect(array('action' => 'index'));
		}
	}
	function admin_mutildelete($str = null){
        if($str){
            $arrid=explode(',',$str);
        }else{
    		$this->Session->setFlash(__('Có lỗi sảy ra. Xóa không thành công !', true));
    		$this->redirect(array('action' => 'index',$str));
        }
         foreach($arrid as $item){
			$this->_delimg($item);
    		$this->Img->delete($item);
         }
		$this->Session->setFlash(__('Đã xóa !', true));
		$this->redirect(array('action' => 'index',$str));
    }   
}